﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    public class GroupDayAvg
    {
        public int Id { get; set; }

        /// <summary>
        /// 小组编号
        /// </summary>
        public int GroupId { get; set; }

        public virtual Sys.Groups Group { get; set; }

        /// <summary>
        /// 交易月
        /// </summary>
        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        /// <summary>
        /// 获取小组均值
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public GroupDayAvg GetGroupAvg(int groupid, int tradeHistoryId, DB.DBManager db)
        {
            Sys.TradeHistory th = new Sys.TradeHistory();
            th = th.GetModel(tradeHistoryId, db);
            var query = db.GroupDayAvg.Where(a => a.GroupId == groupid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new GroupDayAvg();
            }
        }



        /// <summary>
        /// 初始化小组均值
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public void InitGroupAvg(int tradeHistoryId, DB.DBManager db)
        {
            var groups = db.Groups.ToList();
            foreach (Sys.Groups g in groups)
            {
                Sys.DayGroupOperators gops = new Sys.DayGroupOperators();
                List<Sys.Operator> operators = gops.GetGroupOperators(g.Id, new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), db);
                List<Sys.Account> accounts = new Sys.Account().GetOperatorsAccount(operators, tradeHistoryId);
                GroupDayAvg change = new GroupDayAvg();
                if (accounts.Count > 0)
                {
                    foreach (Sys.Account account in accounts)
                    {
                        var query = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query.Count() > 0)
                        {
                            change.BillCount += query.First().BillCount;
                            change.DayCount += query.First().DayCount;
                            change.Free += query.First().Free;
                            change.LoseCount += query.First().LoseCount;
                            change.GroupId = g.Id;
                            change.Profit += query.First().Profit;
                            change.SumLost += query.First().SumLost;
                            change.SumWin += query.First().SumWin;
                            change.UsedMargin += query.First().UsedMargin;
                            change.WinCount += query.First().WinCount;
                        }


                        var _query = from t in db.AccountDayAnalysis where t.AccountId == account.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (_query.Count() > 0)
                        {
                            change.AvgBillBenefit += _query.First().AvgBillBenefit;
                            change.AvgPostionTime += _query.First().AvgPostionTime;
                            change.MaxReturn += _query.First().MaxReturn;
                            change.OverStopLoss += _query.First().OverStopLoss;
                            change.PingJinPing += _query.First().PingJinPing;
                            change.RaiseRate += _query.First().RaiseRate;
                            change.RiskCount += _query.First().RiskCount;
                            change.Utilization += _query.First().Utilization;
                            change.WinRate += _query.First().WinRate;
                            change.Yield += _query.First().Yield;
                        }
                    }
                }
                int operatorCount = operators.Count();
                if (operatorCount > 0)
                {
                    change.BillCount = Math.Round(change.BillCount / operatorCount, 2);
                    change.DayCount = Math.Round(change.DayCount / operatorCount, 2);
                    change.Free = Math.Round(change.Free / operatorCount, 2);
                    change.LoseCount = Convert.ToInt32(change.LoseCount / operatorCount);
                    change.GroupId = g.Id;
                    change.Profit = Math.Round(change.Profit / operatorCount, 2);
                    change.SumLost = Math.Round(change.SumLost / operatorCount, 2);
                    change.SumWin = Math.Round(change.SumWin / operatorCount, 2);
                    change.UsedMargin = Math.Round(change.UsedMargin / operatorCount, 2);
                    change.WinCount = Convert.ToInt32(change.WinCount / operatorCount);
                    change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / operatorCount, 2);
                    change.AvgPostionTime = Math.Round(change.AvgPostionTime / operatorCount, 2);
                    change.MaxReturn = Math.Round(change.MaxReturn / operatorCount, 2);
                    change.OverStopLoss = Math.Round(change.OverStopLoss / operatorCount, 2);
                    change.PingJinPing = Convert.ToInt32(change.PingJinPing / operatorCount);
                    change.RaiseRate = Math.Round(change.RaiseRate / operatorCount, 2);
                    change.RiskCount = Convert.ToInt32(change.RiskCount / operatorCount);
                    change.Utilization = Math.Round(change.Utilization / operatorCount, 2);
                    change.WinRate = Math.Round(change.WinRate / operatorCount, 2);
                    change.Yield += Math.Round(change.Yield / operatorCount, 2);
                    change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / operatorCount, 2);
                    change.AvgPostionTime = Math.Round(change.AvgPostionTime / operatorCount, 2);
                    change.Utilization = Math.Round(change.Utilization / operatorCount, 2);
                    change.WinRate = Math.Round(change.WinRate / operatorCount, 2);
                    change.TradeHistoryId = tradeHistoryId;
                }
                db.GroupDayAvg.Add(change);
            }
            db.SaveChanges();
        }


        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.GroupDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (GroupDayAvg a in query)
            {
                db.GroupDayAvg.Remove(a);
            }
            db.SaveChanges();
        }
    }
}
